// pages/NoDataPage/NoDataPage.js
var api = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
   tuangouID:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    that.data.tuangouID = options.tuangouID;
    that.setData({
      isIphoneX: getApp().globalData.statusBarHeight>=44?true:false,
    })
  },

  moreTuangou:function(){

    var that = this;
    api.request1({ "path": "user/tuangouMore","tuangouId":that.data.tuangouID,"token":wx.getStorageSync('token')}, function (result) {     
      if(result.code=='0'){
        if(result.data.length>0){
          wx.navigateTo({
            url: '/pages/OtherTuangou/OtherTuangou?tuangouID='+that.data.tuangouID,
          })
        }
      }else if(result.code=='301'){
        let id = '582-4Cq9kZL_ka822Yhgm-QnWljGvbFBf_QIklDYtbs'
          wx.getSetting({
            withSubscriptions: true,
            success(res) {
              console.log(res)
              if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
                if (res.subscriptionsSetting.itemSettings) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
                  let moIdState = res.subscriptionsSetting.itemSettings[id]; // 用户同意的消息模板id
                  if (moIdState === 'accept') {
                    getApp().globalData.isCheck1 = 1
                    console.log('接受了消息推送');
    
                    //弹出自定义
                    wx.showModal({
                      title: '',
                      content: '还未有其他团购上线，请关注小程序订阅消息 ',
                      showCancel: false,
                      cancelText:'',
                      confirmText:'我知道了',
                      complete: (res) => {
                        if (res.cancel) {
                          
                        }
                    
                        if (res.confirm) {
                          wx.requestSubscribeMessage({
                            tmplIds: ['582-4Cq9kZL_ka822Yhgm-QnWljGvbFBf_QIklDYtbs'],
                            success(res) {
                              var result = res["582-4Cq9kZL_ka822Yhgm-QnWljGvbFBf_QIklDYtbs"];
                              that.judgeUserRemind("582-4Cq9kZL_ka822Yhgm-QnWljGvbFBf_QIklDYtbs","3",(result=='accept'?'1':'0'))
                  
                            },
                          })
                        }
                      }
                    })

                  } else if (moIdState === 'reject') {
                    wx.showModal({
                      title: '',
                      content: '您已拒绝接收订阅开团通知，可在设置里打开',
                      showCancel: false,
                      cancelText:'',
                      confirmText:'我知道了',
                      complete: (res) => {
                        if (res.cancel) {
                        }
                        if (res.confirm) { 
                        }
                      }
                    })
                  } else if (moIdState === 'ban') {
                    console.log("已被后台封禁");
                  }else{
                    wx.showModal({
                      title: '',
                      content: '还未有其他团购上线，请关注小程序订阅消息',
                      showCancel: true,
                      cancelText:'取消',
                      confirmText:'订阅消息',
                      complete: (res) => {
                        if (res.cancel) {
                          
                        }
                    
                        if (res.confirm) {
                          wx.requestSubscribeMessage({
                            tmplIds: ['582-4Cq9kZL_ka822Yhgm-QnWljGvbFBf_QIklDYtbs'],
                            success(res) {
                              var result = res["582-4Cq9kZL_ka822Yhgm-QnWljGvbFBf_QIklDYtbs"];
                              that.judgeUserRemind("582-4Cq9kZL_ka822Yhgm-QnWljGvbFBf_QIklDYtbs","3",(result=='accept'?'1':'0'))
                  
                            },
                          })
                        }
                      }
                    })
                  }
                } else {
                  wx.showModal({
                    title: '',
                    content: '还未有其他团购上线，请关注小程序订阅消息',
                    showCancel: true,
                    cancelText:'取消',
                    confirmText:'订阅消息',
                    complete: (res) => {
                      if (res.cancel) {
                        
                      }
                  
                      if (res.confirm) {
                        wx.requestSubscribeMessage({
                          tmplIds: ['582-4Cq9kZL_ka822Yhgm-QnWljGvbFBf_QIklDYtbs'],
                          success(res) {
                            var result = res["582-4Cq9kZL_ka822Yhgm-QnWljGvbFBf_QIklDYtbs"];
                            that.judgeUserRemind("582-4Cq9kZL_ka822Yhgm-QnWljGvbFBf_QIklDYtbs","3",(result=='accept'?'1':'0'))
                
                          },
                        })
                      }
                    }
                  })
                }
              } else {
                wx.openSetting({
                  withSubscriptions: true,
                })
              }
            }
          })
      }
    }, function (error) {
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  judgeUserRemind:function(num,typeNum,yesNo){
    var that = this
    console.log('yesNo==='+yesNo)
    let id = num
    wx.getSetting({
      withSubscriptions: true,
      success(res) {
        console.log(res)
        if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
          if (res.subscriptionsSetting.itemSettings) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
            let moIdState = res.subscriptionsSetting.itemSettings[id]; // 用户同意的消息模板id
            if (moIdState === 'accept') {
              console.log('接受了消息推送');
              api.request1({ "path": "user/remind","tuangouId":that.data.tuangouID,"state":"1","type":typeNum,"token":wx.getStorageSync('token')}, function (result) {     
                if(result.code=='0'){
                }
              }, function (error) {
              })
  
            } else if (moIdState === 'reject') {
              console.log('拒绝了消息推送');
              
            } else if (moIdState === 'ban') {
              console.log("已被后台封禁");
            }else{
              api.request1({ "path": "user/remind","tuangouId":that.data.tuangouID,"state":"1","type":typeNum,"token":wx.getStorageSync('token')}, function (result) {     
                if(result.code=='0'){
                }
              }, function (error) {
              })
            }
          } else {
            api.request1({ "path": "user/remind","tuangouId":that.data.tuangouID,"state":"0","type":typeNum,"token":wx.getStorageSync('token')}, function (result) {     
              if(result.code=='0'){
              }
            }, function (error) {
            })
          }
        } else {
          wx.openSetting({
            withSubscriptions: true,
          })
        }
      }
    })
  },
  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})